<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="{{$path}}/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{$path}}/css/public.css" media="all">
    <link rel="stylesheet" href="{{$path}}/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{$path}}/css/layuimini.css?v=2.0.4.2" media="all">
    <link rel="stylesheet" href="{{$path}}/css/themes/default.css" media="all">
    <link rel="stylesheet" href="{{$path}}/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
        /*.layui-table-cell{*/
        /*display:table-cell;*/
        /*vertical-align: middle;*/
        /*}*/
        able{table-layout: fixed;}
        td{word-break: break-all; word-wrap:break-word;}
        .layui-fluid {
            height:auto;
        }
        .layui-table-cell  {
            height:auto;
            overflow:visible;
            text-overflow:inherit;
            white-space:pre-line;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="table_form">

                    <div class="layui-form-item">
                        {{foreach $search as $value}}
                            {{if $value['type']=='input'}}
                                        <div class="layui-inline">
                                    <label class="layui-form-label">{{$value['desc']}}</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="{{$value['name']}}" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            {{elseif $value['type']=='time'}}
                        <div class="layui-inline">
                            <label class="layui-form-label">{{$value['desc']}}</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input date" name="{{$value['name']}}[start]">

                            </div>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input date" name="{{$value['name']}}[end]">

                            </div>
                        </div>
                        {{elseif $value['type']=='select'}}
                        <div class="layui-inline">
                            <label class="layui-form-label">{{$value['desc']}}</label>
                            <div class="layui-input-inline">
                                <select name="{{$value['name']}}" class="layui-select" lay-search>
                                    <option value="">请选择</option>
                                    {{foreach $value['option'] as $vs}}
                                        {{if $value['hierarchy']==1}}
                                    <option value="{{$vs}}">{{$vs}}</option>
                                        {{else}}
                                    <option value="{{$vs[$value['select_value']['id']]}}">{{$vs[$value['select_value']['show']]}}</option>
                                        {{/if}}
                                    {{/foreach}}
                                </select>
                            </div>
                        </div>
                            {{/if}}
                        {{/foreach}}

                        <div class="layui-inline">
                                {{if count($search)}}
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                {{/if}}
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btns"><i class="layui-icon"></i>刷新</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                {{foreach $headtool as $v}}
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" style="{{$v['style']}}" data-action="{{$v['action']}}" data-type="{{$v['type']}}" lay-event="as"> {{$v['desc']}} </button>
                {{/foreach}}
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            {{foreach $linebutton as $value}}
            <a class="layui-btn layui-btn-xs data-count-delete" style="{{$value['style']}}" lay-event="abs" data-type="{{$value['type']}}" data-action="{{$value['action']}}" data-desc="{{$value['desc']}}">{{$value['desc']}}</a>
            {{/foreach}}
        </script>
    </div>
</div>
</body>
<script src="{{$path}}/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="{{$path}}/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table;

        table.render({
            elem: '#currentTableId',url: '{{$action}}',
            toolbar: '#toolbarDemo',
            {{if $page['type']}}
            page: true,
            {{else}}
            page: false,
            {{/if}}
            limits: [
                {{foreach $page['limits'] as $limis}}
            {{$limis}},
                {{/foreach}}
            ],
            limit: {{$page['limit']}},
                    {{if count($total)}}
                    totalRow:true,
                        {{/if}}
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50,align: 'center',},
                        {{foreach $columns as $k=>$val}}
                    {{if $k==0}}
                        {{if count($total)}}
                    {field: '{{$val['name']}}', title: '{{$val['desc']}}',align: 'center', sort: true,totalRowText:'合计'},
                        {{else}}
                    {field: '{{$val['name']}}',  title: '{{$val['desc']}}',align: 'center', sort: true},
                        {{/if}}
                    {{else}}
                            {{if in_array($val['name'],$total)}}
                            {field: '{{$val['name']}}', title: '{{$val['desc']}}',sort:true,align: 'center',totalRow: true},
                            {{else}}
                            {field: '{{$val['name']}}', title: '{{$val['desc']}}',sort:true,align: 'center'},
                            {{/if}}
                    {{/if}}
                    {{/foreach}}
                        {title: '操作', minWidth: 150,maxWidth:200, toolbar: '#currentTableBar', align: "center"}
            ]],
            done:function () {
                // layer.msg('加载完成')
            }
            // skin: 'line'
        });
        function table_reload(){
            var t = $('#table_form').serializeArray();
            var page = $('.layui-laypage-curr').children('em').text()
            console.log(t)
            {{if $page['type']}}
            table.reload('currentTableId', {page: {
                    curr: page
                }
            }, 'data');
            {{else}}
            table.reload('currentTableId', {page: false
            }, 'data');
            {{/if}}
        }
        $('.date').each(function() {
            laydate.render({
                elem: this,
                type:"datetime",
//            value:new Date(),
                istime: true,
            });
        })

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            var tiaojian = {};
            $.each(data.field,function (i,v) {
                tiaojian[i] = v;
            })
            console.log(tiaojian)
            {{if $page['type']}}
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: tiaojian
            }, 'data');
            {{else}}
            table.reload('currentTableId', {
                page: false
                , where: tiaojian
            }, 'data');
            {{/if}}
            return false;
        });
        form.on('submit(data-search-btns)', function (data) {
            location.reload();

            return false;
        });
        /**
         * 监听开关
         * */
        form.on('switch(sexDemo)',function (data) {
            console.log(data);
            var status = data.elem.checked;
            // console.log(status);
            var id = data.value;
            var field = $(this).attr('field');
            $.post('{:url("command.fileadmin/change")}',{id:id,field:field},function (res) {
                if(res.code==200){
                    layui.layer.msg(res.msg);
                }else{
                    layui.layer.msg(res.msg);
                    // table_reload();
                    data.elem.checked = !status;
                }
            })
        })

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            console.log(obj)
            console.log($(this))
            var type = $(this).context.dataset.type;
            var action = $(this).context.dataset.action;
            console.log(type)
            console.log(action)
            if(type=='button'){
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var data_id = new Array();
                $.each(data,function (i,v) {
                    data_id.push(v.id);
                })
                layer.confirm('真的执行这个操作吗？', function (index) {
                    // console.log(data);
                    var id = data_id;
                    $.post(action,{id:id},function (res) {
                        console.log(res);
                        if(res.code==200){
                            layer.msg(res.msg);
                        }else{
                            layer.msg(res.msg);
                        }
                        table_reload();
                    })

                });
            }else if(type=='a'){
                var index = layer.open({
                    title: '添加文件管理',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['90%', '90%'],content: action,end:function () {
                        //执行搜索重载
                        table_reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            console.log(obj)
            console.log($(this))
            var type = $(this).context.dataset.type;
            var action = $(this).context.dataset.action;
            var desc = $(this).context.dataset.desc;
            var data = obj.data;
            if(obj.event=='abs'){
                if(type=='button'){
                    layer.confirm('真的执行这个操作吗？', function (index) {
                        // console.log(data);
                        var id = data.id;
                        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                        $.post(action,{id:id},function (res) {
                            layer.close(index)
                            console.log(res);
                            if(res.code==200){
                                layer.msg(res.msg);
                                table_reload();
                                // obj.del();
                                // layer.close(index);
                            }else{
                                layer.msg(res.msg);
                            }
                        })
                        // obj.del();
                        // layer.close(index);
                    });
                }else if(type=='a'){
                    var id = data.id;
                    id = 'id='+id;
                    if(action.indexOf('?')==-1){
                        action = action+'?'+id;
                    }else{
                        action = action+'&'+id;
                    }
                    var index = layer.open({
                        title: desc,
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['90%', '90%'],content: action,end:function(){table_reload();},});
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }
            }
        });

    });
</script>
</html>